<?php
namespace app\api\controller;

use app\api\controller\BaseController;
use app\api\model\WorkOrder as WorkOrderModel;
use think\Db;

class Workorder extends BaseController
{
    /**
     * 施工订单页面
     * @param  string $status     [description]
     * @param  string $page_index [description]
     * @param  string $page_size  [description]
     * @return [type]             [description]
     */
    public function order_list($status = '', $page_index = '1', $page_size = '6')
    {
        // 获取用户信息
        $userInfo = getTokenInfo();
        if (!$userInfo) 
            return base_result(null, false, '10001', '请登录');
        $user_id = $userInfo->user_id;
        // $user_id = 1;

        $WorkOrder = new WorkOrderModel;
        $where = ['cw.constructors_id' => $user_id];
        if ($status != '') {
            switch ($status) {
                case '1':
                    $where['status'] = $status;
                    break;
                default:
                    $where['status'] = ['neq', 1];
                    break;
            }
        }
        $unhandled = $WorkOrder->where('status', 1)->value('id');
        if ($unhandled) {
            $return['unhandled'] = true;
        } else {
            $return['unhandled'] = false;
        }

        $order = $WorkOrder->alias('w')
        			->where($where)
        			->join('constructors_relation_worker cw', 'w.id = cw.woker_order_id')
        			->field('w.id, w.title, w.type, w.status, w.add_time, w.accept_time, w.doing_time, w.complete_time, cw.add_time as appointed_time')
                    ->order('last_time desc, id')
        			->page("{$page_index},{$page_size}")
        			->select();
                    // dump(json_decode(json_encode($order)));die;
        foreach ($order as $key => $value) {
            $order[$key]['appointed_time'] = date('y.m.d H:i', strtotime($value['appointed_time']));
            $order[$key]['accept_time'] = date('y.m.d H:i', strtotime($value['accept_time']));
            $order[$key]['doing_time'] = date('y.m.d H:i', strtotime($value['doing_time']));
            $order[$key]['complete_time'] = date('y.m.d H:i', strtotime($value['complete_time']));

            if ($value['status'] == 5) {
                $whereOss = ['pid' => $value['id'], 'tab' => 'constructors', 'oss_type' => '5'];
                $imgs = Db::table('aliyun_oss')->where($whereOss)->field('name')->select();
                if ($imgs) {
                    foreach ($imgs as $k => $val) {
                        $image[$k] = config('ali_cdn_url').'/'.$val['name'].'?'.config('ali_adv_size');
                    }
                    $order[$key]['image'] = $image;
                }
            }
        }
        $return['order'] = $order;
        return base_result($return, true);
    }

    /**
     * 订单状态
     * @param  string $order_id [description]
     * @return [type]           [description]
     */
    public function order_info($order_id = '')
    {   
        // 获取用户信息
        $userInfo = getTokenInfo();
        if (!$userInfo) 
            return base_result(null, false, '10001', '请登录');
        $user_id = $userInfo->user_id;
        // $user_id = 1;
        if (!$order_id) {
            return base_result(null, false, 30002, "ID参数为空");
        }
        $WorkOrder = new WorkOrderModel;
        $order_info = $WorkOrder->alias('w')->join('constructors_relation_worker cw', 'w.id = cw.woker_order_id')->where('w.id', $order_id)->field('w.id, w.number, w.title, w.type, w.status, w.add_time, cw.add_time as appointed_time, w.accept_time, w.doing_time, w.complete_time')->find();
        $order_info['appointed_time'] = $order_info['appointed_time'] ? date('y.m.d H:i', strtotime($order_info['appointed_time'])) : '';
        $order_info['accept_time'] = $order_info['accept_time'] ? date('y.m.d H:i', strtotime($order_info['accept_time'])) : '';
        $order_info['doing_time'] = $order_info['doing_time'] ? date('y.m.d H:i', strtotime($order_info['doing_time'])) : '';
        $order_info['complete_time'] = $order_info['complete_time'] ? date('y.m.d H:i', strtotime($order_info['complete_time'])) : '';
        // dump(json_decode(json_encode($order_info)));
        $sql = "SELECT name, oss_type FROM aliyun_oss where tab = 'constructors' and pid = " . $order_id . "";
        $query = Db::query($sql);
        $img = [];
        if ($query) {
            foreach ($query as $key => $value) {
                if ($value['oss_type'] == 3) {
                    $img['3'][] = config('ali_cdn_url').'/'.$value['name'].'?'.config('ali_adv_size');
                } elseif ($value['oss_type'] == 4) {
                    $img['4'][] = config('ali_cdn_url').'/'.$value['name'].'?'.config('ali_adv_size');
                } elseif ($value['oss_type'] == 5) {
                    $img['5'][] = config('ali_cdn_url').'/'.$value['name'].'?'.config('ali_adv_size');
                }
            }
        }
        // dump($img);
        $order_info['imgs'] = $img;
        return base_result($order_info, true);
    }

    /**
     * 更新状态
     * @param string $order_id [description]
     * @param string $status   [description]
     * @param string $imgs     [description]
     * @param string $comment  [description]
     */
    public function set_order_status($order_id = '', $status = '', $imgs = '', $comment = '')
    {
        if (!$order_id) {
            return base_result(null, false, 30002, "ID参数为空");
        }
        $WorkOrder = new WorkOrderModel;
        $whereOrder = ['id' => $order_id];
        $data = ['status' => $status, 'last_time' => date('Y-m-d H:i:s')];
        if ($status == 2) {
            $data['accept_time'] = date('Y-m-d H:i:s');
        } else {
            if ($status == 5) {
                $data['complete_time'] = date('Y-m-d H:i:s');
            } elseif ($status == 4) {
                $data['doing_time'] = date('Y-m-d H:i:s');
            } 
            // elseif ($status == 3) {
            //     $data['accept_time'] = date('Y-m-d H:i:s');
            // }
            $info = $WorkOrder->where($whereOrder)->value('comment');
            $info = !$info ? [] : json_decode($info, true);
            $status_name = config('status_name')[$status];
            $new_comment[$status_name]['comment'] = $comment;
            $new_comment[$status_name]['add_time'] = date('Y-m-d H:i:s');
            $data['comment'] = json_encode(array_merge($info, $new_comment));
        }
        // dump($data);
        $WorkOrder->where($whereOrder)->update($data);

        if ($imgs) {
            $img_data = [];
            foreach ($imgs as $key => $value) {
                if ($value) {
                    $img_data[$key] = ['name' => $value, 'oss_type' => $status, 'add_time' => date('Y-m-d H:i:s'), 'pid' => $order_id, 'tab' => 'constructors'];
                }
            }
            Db::table('aliyun_oss')->insertAll($img_data);
        }
        $return['add_time'] = date('y.m.d H:i');
        return base_result($return, true);
    }








}
